var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = 'yyyy-MM-dd hh:mm:ss';
const $baseUrl = 'http://suzuki-dev-finance.haimaiche.net:8999';

function padding(s, len) {
    var len = len - (s + '').length;
    for (var i = 0; i < len; i++) {
        s = '0' + s;
    }
    return s;
};
export default {
    name: 'libs',
    postDeal: function(_json) {
        let params = new URLSearchParams();
        $.each(_json, function(i, val) {
            params.append(i, val);
        });
        return params;
    },
    getUrlSplit(_url, _param) {
        let j = true;
        $.each(_param, function(i, val) {
            console.log(val);
            console.log(i);
            if (j && val) {
                _url += '?' + i + '=' + val;
                j = false;
            } else if (val) {
                _url += '&' + i + '=' + val;
            }
        });
        return _url;
    },
    formatDate: function(date, pattern) {
        if (!date) return;
        pattern = pattern || DEFAULT_PATTERN;
        return pattern.replace(SIGN_REGEXP, function($0) {
            switch ($0.charAt(0)) {
                case 'y':
                    return padding(date.getFullYear(), $0.length);
                case 'M':
                    return padding(date.getMonth() + 1, $0.length);
                case 'd':
                    return padding(date.getDate(), $0.length);
                case 'w':
                    return date.getDay() + 1;
                case 'h':
                    return padding(date.getHours(), $0.length);
                case 'm':
                    return padding(date.getMinutes(), $0.length);
                case 's':
                    return padding(date.getSeconds(), $0.length);
            }
        });
    },
    //金额转大写
    digitUppercase: function(n) {
        var fraction = ['角', '分'];
        var digit = [
            '零', '壹', '贰', '叁', '肆',
            '伍', '陆', '柒', '捌', '玖'
        ];
        var unit = [
            ['元', '万', '亿'],
            ['', '拾', '佰', '仟']
        ];
        var head = n < 0 ? '欠' : '';
        n = Math.abs(n);
        var s = '';
        for (var i = 0; i < fraction.length; i++) {
            s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        n = Math.floor(n);
        for (var i = 0; i < unit[0].length && n > 0; i++) {
            var p = '';
            for (var j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[n % 10] + unit[1][j] + p;
                n = Math.floor(n / 10);
            }
            s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
        }
        return head + s.replace(/(零.)*零元/, '元')
            .replace(/(零.)+/g, '零')
            .replace(/^整$/, '零元整');
    },
    //身份证号合法性验证 
    //支持15位和18位身份证号
    //支持地址编码、出生日期、校验位验证
    IdentityCodeValid: function(code) {
        var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
        var tip = "";
        var pass = true;
        if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
            tip = "身份证号格式错误";
            pass = false;
        } else if (!city[code.substr(0, 2)]) {
            tip = "地址编码错误";
            pass = false;
        } else {
            //18位身份证需要验证最后一位校验位
            if (code.length == 18) {
                code = code.split('');
                //∑(ai×Wi)(mod 11)
                //加权因子
                var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                //校验位
                var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
                var sum = 0;
                var ai = 0;
                var wi = 0;
                for (var i = 0; i < 17; i++) {
                    ai = code[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                var last = parity[sum % 11];
                if (parity[sum % 11] != code[17].toUpperCase()) {
                    tip = "校验位错误";
                    pass = false;
                }
            }
        }
        return pass;
    },
    changeHeight: function(_id, _data) {
        if (_data.shrink) {
            $('#' + _id).addClass('text-height-shrink');
            $('#' + _id).animate({ height: "20px" });
        } else {
            $('#' + _id).removeClass('text-height-shrink');
            $('#' + _id).animate({ height: document.getElementById(_id).scrollHeight + "px" }, 300);
        }
        _data.shrink = !_data.shrink;
        _data.shrinkText = _data.shrink ? '收起' : '展开全部';
    },
    loadLibs: function(_url) {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.open("GET", _url, true);
        xmlhttp.send();
    },
    getPhoneAddress(_cur, _old, _this, _address) {
        console.log(_cur);
        if (_cur.length == 11) {
            _this.$http.get('/location/mobilePhone/' + _cur).then((data) => {
                if (data.data.success) {
                    let datas = data.data.data;
                    _address = _address.split('.');
                    if (_address.length == 1) {
                        _this[_address[0]] = datas;
                    } else if (_address.length == 2) {
                        _this[_address[0]][_address[1]] = datas;
                    } else if (_address.length == 3) {
                        _this[_address[0]][_address[1]][_address[2]] = datas;
                    } else if (_address.length == 4) {
                        _this[_address[0]][_address[1]][_address[2]][_address[3]] = datas;
                    }
                }
            }).catch((error) => {

            });
        } else {
            if (_address.length == 1) {
                _this[_address[0]] = '';
            } else if (_address.length == 2) {
                _this[_address[0]][_address[1]] = '';
            } else if (_address.length == 3) {
                _this[_address[0]][_address[1]][_address[2]] = '';
            } else if (_address.length == 4) {
                _this[_address[0]][_address[1]][_address[2]][_address[3]] = '';
            }
            console.log('none phone address');
        }
    },
    mustWrite(_json, _noMust) {
        console.log(_json);
        let isAll = true;
        $.each(_json, function(index, vm) {
            if (typeof _json[index] == 'object') {
                $.each(_json[index], function(i, val) {
                    if (val == undefined) {
                        _json[index][i] = null;
                    }
                    if (typeof val == 'object') {
                        $.each(val, function(j, v) {
                            if (v == undefined) {
                                _json[index][i][j] = null;
                            }
                        });
                    }

                });
            }
        });
        $.each(_json, function(i, el) {
            if (typeof _json[i] == 'object') {
                $.each(el, function(j, val) {
                    if (typeof val == 'object') {
                        $.each(val, function(t, va) {
                            if (!_json[i][j][t] && _json[i][j][t] != '0') {
                                if (_noMust) {
                                    let isHaveNo = false;
                                    $.each(_noMust, function(index, v) {
                                        if (v.indexOf('.') != -1) {
                                            let noMustArr = v.split('.');
                                            if (i == noMustArr[0] && j == noMustArr[1] && t == noMustArr[2]) {
                                                isHaveNo = true;
                                                console.log('空-' + i + '-' + j + '-' + index);
                                            }
                                        }
                                    });
                                    if (!isHaveNo) {
                                        isAll = false;
                                    }
                                } else {
                                    isAll = false;
                                    console.log('空-' + i + '-' + j);
                                }
                            }
                        });
                    } else {
                        if (!_json[i][j] && _json[i][j] != '0') {
                            if (_noMust) {
                                let isHaveNo = false;
                                $.each(_noMust, function(index, v) {
                                    if (v.indexOf('.') != -1) {
                                        let noMustArr = v.split('.');
                                        if (i == noMustArr[0] && j == noMustArr[1]) {
                                            isHaveNo = true;
                                            console.log('空-' + i + '-' + j + '-' + index);
                                        }
                                    }
                                });
                                if (!isHaveNo) {
                                    isAll = false;
                                }
                            } else {
                                isAll = false;
                                console.log('空-' + i + '-' + j);
                            }
                        }
                    }

                });
            } else {
                if (!_json[i] && _json[i] != '0') {
                    if (_noMust) {
                        let isHaveNo = false;
                        $.each(_noMust, function(index, v) {
                            if (v.indexOf('.') == -1) {
                                if (i == v) {
                                    isHaveNo = true;
                                    // isAll = false;
                                    console.log('空-' + i + '-' + index);
                                }
                            }
                        });
                        if (!isHaveNo) {
                            isAll = false;
                        }
                    } else {
                        isAll = false;
                        console.log('空-' + i);
                    }
                }
            }
        });
        return isAll;
    },
    successAlert(_this, _json, cb) {
        // _json格式
        // {
        //     title:'',
        //     theme:'',
        //     enter:''
        // }
        _this.$alert(_json.title, _json.theme, {
            confirmButtonText: _json.enter,
            type: _json.type ? _json.type : 'success',
            callback: action => {
                if (cb) {
                    cb();
                } else if (_json.cb) {
                    _json.cb();
                }
            }
        });
    },
    //获取cookie、
    getCookie: function(name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return (arr[2]);
        else
            return null;
    },

    //设置cookie,增加到vue实例方便全局调用
    setCookie: function(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    },

    //删除cookie
    delCookie: function(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = getCookie(name);
        if (cval != null)
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    },
    jsonGetName: function(_json, _id) {
        let name;
        $.each(_json, (i, val) => {
            if (val.value == _id) {
                name = val.label ? val.label : (val.text ? val.text : val.address);
                return;
            }
        });
        return name;
    },
    jsonGetId: function(_json, _name) {
        let id;
        $.each(_json, (i, val) => {
            if (val.label == _name || val.text == _name || val.address == _name) {
                id = val.value;
                return;
            }
        });
        return id;
    },
    getDictionary(_state, _this, _json) {
        if (!_this.this) {
            _this = {
                this: _this
            }
        }
        _this.this.$http.get(_json.url).then((data) => {
            if (data.data.success) {
                _state[_json.key] = [];
                $.each(data.data.data, function(i, val) {
                    if (_json.filter) {
                        for (let j = 0; j < _json.filter.length; j++) {
                            if (_json.filter[j] == val[_json.value]) {
                                if (_json.value && _json.value == 'code') {
                                    _state[_json.key].push({
                                        text: _json.label ? val[_json.label] : val.name,
                                        label: _json.label ? val[_json.label] : val.name,
                                        address: _json.label ? val[_json.label] : val.name,
                                        value: parseInt(val[_json.value]),
                                    });
                                } else {
                                    _state[_json.key].push({
                                        text: _json.label ? val[_json.label] : val.name,
                                        label: _json.label ? val[_json.label] : val.name,
                                        address: _json.label ? val[_json.label] : val.name,
                                        value: _json.value ? val[_json.value] : val.dataId,
                                    });
                                }
                            }
                        }
                    } else {
                        if (_json.value && _json.value == 'code') {
                            _state[_json.key].push({
                                text: _json.label ? val[_json.label] : val.name,
                                label: _json.label ? val[_json.label] : val.name,
                                address: _json.label ? val[_json.label] : val.name,
                                value: parseInt(val[_json.value]),
                            });
                        } else {
                            _state[_json.key].push({
                                text: _json.label ? val[_json.label] : val.name,
                                label: _json.label ? val[_json.label] : val.name,
                                address: _json.label ? val[_json.label] : val.name,
                                value: _json.value ? val[_json.value] : val.dataId,
                            });
                        }
                    }
                });
                if (_this.cb) {
                    _this.cb();
                }
            }
        }).catch((error) => {

        });
    },
    toInt(_data) {
        return (parseInt(_data) || parseInt(_data) == 0 || parseInt(_data) == false) ? parseInt(_data) : ''
    },
    noneAddHightBorder() {
        $('.info-input').removeClass('redBorder');
        $('.info-select').removeClass('redBorder');
        for (let i = 0; i < $('.el-input__inner').length; i++) {
            let vm = $('.el-input__inner')[i];
            if (!vm.value && vm.disabled == false && vm.type == 'text') {
                for (let j = 0; j < $(vm).parents('.info-input').length; j++) {
                    $($(vm).parents('.info-input')[j]).addClass('redBorder');
                }
                for (let j = 0; j < $(vm).parents('.info-select').length; j++) {
                    $($(vm).parents('.info-select')[j]).addClass('redBorder');

                }
                $('.isOptional').removeClass('redBorder');
            }
        }
        $("input").blur(function() {
            if (this.value) {
                $($(this).parents('.info-input')[0]).removeClass('redBorder');
                $($(this).parents('.info-select')[0]).removeClass('redBorder');
            }
        });
    },
    scrollTop(_value) {
        console.log($(_value));
        for (let i = 0; i < $(_value).length; i++) {
            console.log($($(_value)[i])[0]);
            $($(_value)[i])[0].scrollTop = 0;
        }
    }
}
